﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.DAL.LocalDataAccess/BoxStateAccess
* 类       名: BoxStateAccess
* 功      能:  TD
* 时      间:  2024-05-21 10:19:48
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using log4net.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.Extensions;
using WaferSelectV3.Services;

namespace WaferSelectV3.DAL.LocalDataAccess
{
    public class BoxStateAccess
    {
        /// <summary>
        /// 获取所有的料盒状态数据
        /// </summary>
        /// <returns></returns>
        public static DataResult<List<BoxStateEntity>> GetBoxStates()
        {
            DataResult<List<BoxStateEntity>> result = new DataResult<List<BoxStateEntity>>();
            try
            {
                using (var dataContext = new DataContext())
                {
                    result.Data = dataContext.BoxStates.OrderBy(x => x.BoxNumber).ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }

            return result;
        }

        /// <summary>
        /// 数据库内容初始化
        /// </summary>
        /// <returns></returns>
        public static DataResult DataInit()
        {
            DataResult result = new DataResult();
            try
            {
                List<BoxStateEntity> initData = new List<BoxStateEntity>()
                {
                    new BoxStateEntity() {BoxNumber = 1,BoxLockedSeconds=5,UpCount = 0,DownCount = 0,CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 2,BoxLockedSeconds=5,UpCount = 0,DownCount = 0,CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 3,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 4,BoxLockedSeconds=5,UpCount = 0,DownCount = 0,CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 5,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 6,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 7,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 8,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 9,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 10,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 11,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 12,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 13,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 14,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 15,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 16,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 17,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 18,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 100},
                    new BoxStateEntity() {BoxNumber = 19,BoxLockedSeconds=5,UpCount = 0,DownCount = 0, CountMax = 10000},
                };

                foreach (var entity in initData)
                {
                    var addResult = AddDataSingle(entity);
                    if (!addResult.State)
                    {
                        return addResult;
                    }
                }
                result.State = true;
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        public static DataResult AddDataSingle(BoxStateEntity entity)
        {
            DataResult result = new DataResult();
            try
            {
                using (var dataContext = new DataContext())
                {
                    dataContext.BoxStates.Add(entity);
                    int affectedRows = dataContext.SaveChanges();
                    if (affectedRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantProvider.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

    }
}
